/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gem.model.core;

import gem.model.dao.DAOFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Dusan
 */
public class SimpleDepartmentCataloqueModel implements DepartmentCatalogueModel
{

    private DAOFactory factory;
    private java.util.Collection< Department > deps = null;
    private java.util.ArrayList< DepartmentListener > listeners;

    private void reloadDepartments() throws java.sql.SQLException
    {
        loadDepartmentsFully();
    }
    private void loadDepartmentsFully() throws java.sql.SQLException
    {
        deps = factory.getDepartmentDAO().listDepartments();

        for (Department d : deps)
        {
            java.util.Collection<ScienceDomain> sdomains = factory.getDepartmentDAO().listScienceDomains( d );

            d.setScienceDomains( new ArrayList< ScienceDomain >( sdomains.size()) );

            for (ScienceDomain sd : sdomains)
            {
                java.util.Collection< Course > courses = factory.getScienceDomainDAO().listCourses( sd );
                sd.setCourses( new ArrayList< Course >( courses.size() ));
                for( Course c : courses )
                {
                    sd.addCourse(c);
                }

                d.addScienceDomain(sd);
            }            
        }
        
    }
    public SimpleDepartmentCataloqueModel() throws java.sql.SQLException
    {
        factory = DAOFactory.getDAOFactory();
        if( factory != null )
        {
            loadDepartmentsFully();
        }
        listeners = new ArrayList< DepartmentListener >();
    }

    public void addNewCourse(ScienceDomain parent, String name) throws SQLException
    {
        Course c = new Course().setTitle(name);
        parent.addCourse(c);
        reloadDepartments();
        notifyListeners();
    }

    public void addNewDepartment(String s) throws SQLException
    {
        Department d = new Department().setTitle(s);
        reloadDepartments();
        notifyListeners();
    }

    public void addNewScienceDomain(Department d, String name ) throws SQLException
    {
        ScienceDomain sd = new ScienceDomain().setTitle(name);
        d.addScienceDomain(sd);
        factory.getScienceDomainDAO().insertScienceDomain(sd);
        reloadDepartments();
        notifyListeners();
    }

    public void addDepartmentListener( DepartmentListener dl )
    {
        listeners.add( dl );
    }

    public void addNewCourse(Course c) throws SQLException
    {
        factory.getCourseDAO().insertCourse( c );
        reloadDepartments();
        notifyListeners();
    }

    public void addNewDepartment(Department d) throws SQLException
    {
        factory.getDepartmentDAO().insertDepartment( d );
        reloadDepartments();
        notifyListeners();
    }

    public void addNewScienceDomain(ScienceDomain sd) throws SQLException
    {
        factory.getScienceDomainDAO().insertScienceDomain(sd);
        reloadDepartments();
        notifyListeners();
    }

    public void deleteCourse(Course c) throws SQLException
    {
        factory.getCourseDAO().deleteCourse(c);
        reloadDepartments();
        notifyListeners();
    }

    public void deleteDepartment(Department d) throws SQLException
    {
        factory.getDepartmentDAO().deleteDepartment(d);
        reloadDepartments();
        notifyListeners();
    }

    public void deleteScienceDomain(ScienceDomain sd) throws SQLException
    {
        factory.getScienceDomainDAO().deleteScienceDomain(sd);
        reloadDepartments();
        notifyListeners();
    }

    public Course findCourse(String name) throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Department findDepartment(String name) throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ScienceDomain findScienceDomain(String name) throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Course getCourse(ScienceDomain parent, int index) throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void renameCourse(Course c) throws SQLException
    {
        factory.getCourseDAO().updateCourse(c);
        reloadDepartments();
        notifyListeners();
    }

    public void renameDepartment(Department d) throws SQLException
    {
        factory.getDepartmentDAO().updateDepartment(d);
        reloadDepartments();
        notifyListeners();
    }

    public void renameScienceDomain(ScienceDomain sd) throws SQLException
    {
        factory.getScienceDomainDAO();
        reloadDepartments();
        notifyListeners();
    }

    public Department getDepartment(int index) throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ScienceDomain getScienceDomain(Department parent, int index) throws SQLException
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Collection<Department> listDepartments( boolean reload ) throws java.sql.SQLException
    {
        if( reload )
        {
            reloadDepartments();
        }
        return deps;
    }

    public void moveCourse(Course c, ScienceDomain newsDomain) throws SQLException
    {
        factory.getScienceDomainDAO().moveCourse(c, c.getScienceDomain(), newsDomain);
        reloadDepartments();
        notifyListeners();
    }

    public void moveScienceDomain(ScienceDomain sd, Department newDepartment) throws SQLException
    {
        factory.getDepartmentDAO().moveScienceDomain(sd, sd.getDepartment(), newDepartment);
        reloadDepartments();
        notifyListeners();
    }

    public void removeDepartmentListener( DepartmentListener dl )
    {
        listeners.remove(dl);
    }

    private void notifyListeners()
    {
        for( DepartmentListener dl : listeners )
        {
            dl.dataChanged();
        }
    }
}
